#include "netdisk.h"

// 解析路径，生成绝对路径
//      根据输入的路径参数（绝对或相对路径），结合当前工作目录栈，生成完整的绝对路径
void onePath(const char *arg,char * onepath,Stack_t*  dir){
    char  buf[4096]={0} ;
    if(arg[0] == '.'){
        if(arg[1] == '.'){
            travel_stack(*dir,buf);
            if(dir->stackSize == 1){
                strcpy(onepath, buf);
                return;
            }
        //    printf("path=%s",buf);
            int endsize = strlen(dir->pTop->data);
            int bufLen = strlen(buf);
            char path[4096]={0};
            
            strncpy(path,buf,bufLen-endsize);
            strcat(path,&arg[3]);
            strcpy(onepath,path);
        }else if(arg[1] == '/'){
            travel_stack(*dir,buf);
            //printf("path = %s",buf);
            if(arg[2] != '\0'){
                strcat(buf,"/");
            }
            strcat(buf,&arg[2]);
            strcpy(onepath,buf);
        }else{
            travel_stack(*dir, buf);
            strcpy(onepath, buf);
        }
    }else if(arg[0] == '/'){  // 若是用户输入的路径是绝对路径，那么onePath函数从根目录开始构建路径
        char path[4096]={0};
        strcpy(path,dir->pBottom->data); // path = /
        strcat(path,&arg[0]);            // path = /
        strcpy(onepath,path);            // path = /用户输入的路径
    }else if(arg[0] == '\0'){
        travel_stack(*dir, buf);
        strcpy(onepath, buf);
    }
    else{
        travel_stack(*dir,buf); // 遍历栈
        strcat(buf,"/");  // 用于将一个字符串追加到另一个字符串的末尾
        strcat(buf,&arg[0]);
        strcpy(onepath,buf);
    }


}
